<?php
namespace App\Services;

use App\Models\Store;
use Illuminate\Support\Facades\DB;

/**
 *
 * 店铺服务层
 *
 * @author maqu
 *
 */
class StoreService extends BaseService {

    /**
     * 获取店铺配置
     *
     * @return mixed
     */
    public function getStoreConfig($store_id = 1){
        $config =[
            'services'=>[],
            'shipping'=>[
                'shipping_fee' => 0,
                'min_free'     => 0,
            ],
            'expiration_time' => 24,
            'tuan_control' => [],
            'hongbao_control' => [],
            'hongbao_control_time' => [],
            'hongbao_valid_time' => 0,
            'auto_confirm_days' => 0
        ];

        $store_info = Store::find($store_id);
        if($store_info){
            $configs= json_decode($store_info->config);

            $config['services'] =$configs->services;
            $config['shipping'] = (array)$configs->shipping;
            $config['expiration_time'] = $configs->expiration_time;
            $config['tuan_control'] = $configs->tuan_control;
            $config['hongbao_control'] = $configs->hongbao_control;
            $config['hongbao_control_time'] = (array)$configs->hongbao_control_time;
            $config['hongbao_valid_time'] = $configs->hongbao_valid_time;
            $config['auto_confirm_days'] = $configs->auto_confirm_days;
        }
        unset($configs);
        return $config;
    }

    /**
     * 根据城市获取推荐乡村
     *
     * @param $city 定位
     * @param $level 等级
     * @param $firstrow
     * @param $pagesize
     * @return array
     */
    public function getRecommendStores($province,$city,$country,$firstrow,$pagesize){

        $query = Store::where('state',STORE_STATE_OPENING);

//        if($level=='city'){
//            $stores->where('city','like','%'.$location['city'].'%');
//        }else{
//            $stores->where('country','like','%'.$location['district'].'%');
//        }
        if($province){
            $query->where('province',$province);
        }
        if($city){
            $query->where('city',$city);
        }
        if($country){
            $query->where('country',$country);
        }
        $query->select('store_id','store_name','store_banner');
        $query->orderBy('recommended','desc');
//        $query->skip($firstrow)->take($pagesize);
        $stores =$query->get();
        foreach($stores as $item){
            $item->store_banner = complete_url($item->store_banner);
        }

        return $this->success($stores->toArray());

    }

}